import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import { utils } from "../../utils/CommonUtil";
import MoveObj from "../Prefabs/InteractivObj/Common/MoveObj";
import RotateObj from "../Prefabs/InteractivObj/Common/RotateObj";
import GamePropConst from "../../constant/GamePropConst";
import Sound from "../../constant/SoundNameConstant";
import DialogDataConst from "../../constant/DialogDataConst";
import EventNameList from "../../constant/EventNameList";

export default class LevelControl5 extends LevelBaseControl{

    //#region room1
    /** 排水管阀门*/
    private valve:Laya.Sprite3D;
    /** 当前是否排水了*/
    private haveDrain:boolean = false;

    /** 水*/
    private water:MoveObj;

    /** 绿色箱子盖*/
    private greenBoxCover:RotateObj;
    /** 荧光墙壁的材质*/
    private fluarWall:Laya.Sprite3D;
    /** 尸体解剖动画*/
    private shitiAnim:Laya.Animator;
    /** 尸体是否已经解剖开了*/
    private isShowShitiAnim:boolean = false;
    /** 模特的手控制脚本*/
    private modelHandRotate:RotateObj;
    /** 模特的手是否打开了*/
    private isModelHandOpen:boolean = false;
    /** 模特缺失的手节点*/
    private modelNoHand:Laya.Sprite3D;
    /** 盒子上的红宝石*/
    private boxRuby:Laya.Sprite3D;
    /** 梯子*/
    private ladder:Laya.Sprite3D;
    /** 梯子是否落下来了*/
    private isLadderDown:boolean = false;
    //#endregion

    protected startByBase(){
        this.InitRoom1();
    }


    private InitRoom1()
    {
        let room1:Laya.Sprite3D = this.allLevelObjs[0].owner as Laya.Sprite3D;
        let walls = room1.getChildByName("walls") as Laya.Sprite3D;
        let wall2 = walls.getChildByName("wall2") as Laya.Sprite3D;
        let wall3 = walls.getChildByName("wall3") as Laya.Sprite3D;
        let container = room1.getChildByName("Container") as Laya.Sprite3D;

        this.valve = wall2.getChildByName("Valve_obj").getChildByName("valve_prop_Water") as Laya.Sprite3D;
        this.water = utils.CommonUtils.CustomAddComponent(container.getChildByName("water"),MoveObj) as MoveObj;

        this.greenBoxCover = utils.CommonUtils.CustomAddComponent(container.getChildByName("boxes")
        .getChildByName("BOX").getChildByName("box_open_props"),RotateObj) as RotateObj;


        this.fluarWall = wall3.getChildByName("GameObject").getChildByName("wall_mima") as Laya.Sprite3D;
        this.fluarWall.active = false;

        
        this.shitiAnim = utils.CommonUtils.CustomAddComponent(container.getChildByName("shiiti_root").getChildByName("shiti"),Laya.Animator) as Laya.Animator;

        let modelRoot = container.getChildByName("model"); 
        this.modelHandRotate = utils.CommonUtils.CustomAddComponent(modelRoot.getChildByName("modle_finger_props"),RotateObj) as RotateObj;
        this.modelNoHand = modelRoot.getChildByName("hand") as Laya.Sprite3D;
        this.boxRuby = container.getChildByName("box_ruby") as Laya.Sprite3D; 
        this.ladder = wall2.getChildByName("ladder").getChildByName("ladder_props") as Laya.Sprite3D;
        
    }

    public  DoInteractObjLogic( _name:string):boolean
    {
        switch (_name)
        {
            /** room1     *********************************************************************************************/
            /** 点击排水管  判断有没有阀门 装阀门*/
            case "takeOnValve":
                if (this.IsSelectProp(GamePropConst.ID_VALVE))
                {
                    this.UseCurProp();
                    this.valve.active = true;
                }
                break;
            /** 点了排水阀门 进行排水*/
            case "valve_prop_Water":
                if (!this.haveDrain)
                {
                    this.water.Interactive();
                    this.haveDrain = true;
                    /** 播放放水声*/
                    window.gameFacade.SoundMng.playSound(Sound.soundClipArr.LA_SHUI_ZHA);
                }
                break;
            /** 点击水下柜子 如果水没放掉 返回false 不能操作*/
            case "Cabinet":
                if (!this.haveDrain)
                {
                    this.ShowDialog(DialogDataConst.need_drain);
                    return false;
                }
                break;
            /** 绿色箱子 打开了*/
            case "BOX_UnLock":
                /** 打开盖子*/
                this.greenBoxCover.Interactive();
                break;
            /** 墙壁荧光记号  是否显示出来*/
            case "wall_props":
                /** 如果有荧光剂 就是用并显示记号*/
                if (this.IsSelectProp(GamePropConst.ID_Fluorescent_agent))
                {
                    this.UseCurProp();
                    /** 慢慢显示记号*/
                    this.fluarWall.active = true;
                }
                break;
            /** 点击尸体 判断有没有电锯*/
            case "shiti":
                /** 如果有电锯 就使用*/
                if (this.IsSelectProp(GamePropConst.ID_Charged_Chainsaw))
                {
                    console.log("ID_Charged_Chainsaw!!!");
                    this.UseCurProp();
                    window.gameFacade.SoundMng.playSound(Sound.soundClipArr.DIAN_JU);
                    /** 播放剖开动画*/
                    this.shitiAnim.play();
                    this.isShowShitiAnim = true;
                }
                break;
            /** 点击尸体 肚子里的模特手 如果解剖过了 就返回true正常获得*/
            case "modle_hand_r_props":
                return this.isShowShitiAnim;
                break;
            /** 点击红宝石 如果模特手已经伸开了 就正常获得*/
            case "crystal_props":
                if (!this.isModelHandOpen)
                {
                    this.ShowDialog(DialogDataConst.unable_take_out);
                    return false;
                }
                break;
            /** 检测放上模特的手*/
            case "check_take_on_hand":
                /** 如果有道具 就放上去*/
                if (this.IsSelectProp(GamePropConst.ID_MODEL_HAND))
                {
                    this.UseCurProp();
                    /** 装上手  并打开手掌*/
                    this.modelNoHand.active = true;
                    this.modelHandRotate.Interactive();
                    this.isModelHandOpen = true;
                }
                break;
            /** 检测选择宝石道具 使用 放到机关盒子上*/
            case "checkOnDiamond":
                /** 如果有道具 就放上去*/
                if (this.IsSelectProp(GamePropConst.ID_RUBY))
                {
                    this.UseCurProp();
                    this.boxRuby.active = true;
                    this.ShowDialog(DialogDataConst.fallen_obj);
                    /** 梯子落下来 事件*/
                    window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,this.PutDownLadderLogic);
                }
                break;
            /**如果梯子放下来了 爬梯子过关*/
            case "ladder":
            case "ladder_props":
                if (this.isLadderDown)
                {
                    this.FinishLevel();
                }
                else
                {
                    this.ShowDialog(DialogDataConst.cant_climp_up);
                }
                break;           
            default:
                break;
        }
        return true;
    }
    /** 相机归位梯子落下来事件*/
    private PutDownLadderLogic( obj:any)
    {
        this.PutDownLadder();
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent,this,this.PutDownLadderLogic)
    }
    /** 梯子落下*/
    private PutDownLadder()
    {
        let targetPos = new Laya.Vector3(0,-4.49,0.22);
        utils.CommonUtils.DOPositionTween(
            this.ladder.transform,
            this.ladder.transform.localPosition,
            targetPos, 1,
            Laya.Handler.create(this, function () {
                console.log("移动到了目标位置了",this.targetPos);
            }),true,false,true)

        this.isLadderDown = true;
    }
    onDestroy()
    {
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent,this,this.PutDownLadderLogic)
    }

}